package com.gmail.ceac13.servicos;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.*;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.SharedPreferences;
import android.util.Log;


public class ConsultaHttp {
	private static String ENDERECO = "http://connectme.scienceontheweb.net/";
	//private static String ENDERECO = "http://10.0.2.2/connectme/";
	public ConsultaHttp() {
		
	}
	private static String getHttpContent(String end, List<NameValuePair> params) {		
		BufferedReader rd = null;
		String line = "";
		String aux = null;

		HttpClient httpClient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(ENDERECO+end);
		//HttpPost httppost = new HttpPost(ENDERECO);

//		List<NameValuePair> params = new ArrayList<NameValuePair>();
//		params.add(new BasicNameValuePair("requisitado", id_requisitado));
		try {
			httppost.setEntity(new UrlEncodedFormEntity(params));

			HttpResponse response = httpClient.execute(httppost);
			HttpEntity entity = response.getEntity();
			InputStream is = entity.getContent();

			rd = new BufferedReader(new InputStreamReader(is));
			aux = rd.readLine();
			while (aux != null) {
				line = line + aux;
				aux = rd.readLine();
			}

		} catch (ClientProtocolException e) {
			Log.e("ERROR", e.getMessage(), e);
		} catch (IOException e) {
			Log.e("ERROR", e.getMessage(), e);
		}
		return line;
	}
	
	private static String getHttpContent(String end, List<NameValuePair> params, String endServidor) {		
		BufferedReader rd = null;
		String line = "";
		String aux = null;

		HttpClient httpClient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(endServidor+end);
		//HttpPost httppost = new HttpPost(ENDERECO);

//		List<NameValuePair> params = new ArrayList<NameValuePair>();
//		params.add(new BasicNameValuePair("requisitado", id_requisitado));
		try {
			httppost.setEntity(new UrlEncodedFormEntity(params));

			HttpResponse response = httpClient.execute(httppost);
			HttpEntity entity = response.getEntity();
			InputStream is = entity.getContent();

			rd = new BufferedReader(new InputStreamReader(is));
			aux = rd.readLine();
			while (aux != null) {
				line = line + aux;
				aux = rd.readLine();
			}

		} catch (ClientProtocolException e) {
			Log.e("ERROR", e.getMessage(), e);
		} catch (IOException e) {
			Log.e("ERROR", e.getMessage(), e);
		}
		return line;
	}
	/*
	public static String listarRequisicoes(String id_requisitado) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("requisitado", id_requisitado));
		return getHttpContent("listRequests.php", params);
	}
	public static String isLogin(String email, String senha) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("email", email));
		params.add(new BasicNameValuePair("senha", senha));

		return getHttpContent("isLogin.php", params);
	}
	public static String registrarUsuario(String nome, String email, String senha) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("email", email));
		params.add(new BasicNameValuePair("senha", senha));
		params.add(new BasicNameValuePair("nome", nome));

		return getHttpContent("newUser.php", params);
	}
	
	public static String buscarUsuarioPorEmail(String email) {
	
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("email_requisitado", email));
		
		return getHttpContent("newRelationship.php", params);
	}
	
	public static String buscarUsuario(String id) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("id", id));
		
		return getHttpContent("buscarFriend.php", params);
	}
	public static String requisitarAmizade(String requisitor, String requisitado) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("requisitor", requisitor));
		params.add(new BasicNameValuePair("email_requisitado", requisitado));
	
		return getHttpContent("newRelationship.php", params);
	}
	public static String responderRequisicao(int user_id, int id, boolean b) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("requisitor", ""+id));
		params.add(new BasicNameValuePair("requisitado", ""+user_id)); 
		int aceitar = 0;
		if (b)
			aceitar = 1;
		params.add(new BasicNameValuePair("aceitar", ""+aceitar));
	
		return getHttpContent("acceptFriend.php", params);
	}
	public static String listarAmigos(String user_id, String posicao) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("posicao", posicao));
		params.add(new BasicNameValuePair("id_usuario", user_id));
		
		return getHttpContent("getFriends.php", params);
	}
	*/
	/** Com endereco **/
	
	public static String listarRequisicoes(String id_requisitado, String endServer) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("requisitado", id_requisitado));
		return getHttpContent("listRequests.php", params, endServer);
	}
	public static String isLogin(String email, String senha, String endServer) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("email", email));
		params.add(new BasicNameValuePair("senha", senha));

		return getHttpContent("isLogin.php", params, endServer);
	}
	public static String registrarUsuario(String nome, String email, String senha, String endServer) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("email", email));
		params.add(new BasicNameValuePair("senha", senha));
		params.add(new BasicNameValuePair("nome", nome));

		return getHttpContent("newUser.php", params, endServer);
	}
	
	public static String buscarUsuarioPorEmail(String email, String endServer) {
	
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("email_requisitado", email));
		
		return getHttpContent("newRelationship.php", params, endServer);
	}
	
	public static String buscarUsuario(String id, String endServer) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("id", id));
		
		return getHttpContent("buscarFriend.php", params, endServer);
	}
	public static String requisitarAmizade(String requisitor, String requisitado, String endServer) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("requisitor", requisitor));
		params.add(new BasicNameValuePair("email_requisitado", requisitado));
	
		return getHttpContent("newRelationship.php", params, endServer);
	}
	public static String responderRequisicao(int user_id, int id, boolean b, String endServer) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("requisitor", ""+id));
		params.add(new BasicNameValuePair("requisitado", ""+user_id)); 
		int aceitar = 0;
		if (b)
			aceitar = 1;
		params.add(new BasicNameValuePair("aceitar", ""+aceitar));
	
		return getHttpContent("acceptFriend.php", params, endServer);
	}
	public static String listarAmigos(String user_id, String posicao, String endServer) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("posicao", posicao));
		params.add(new BasicNameValuePair("id_usuario", user_id));
		
		return getHttpContent("getFriends.php", params, endServer);
	}
	
	
}
